import Vue from 'vue'
import Vuex from 'vuex'
import menu from './modules/menu'
import look from './modules/look' // 表情
import icon from './modules/iconList'
import dictionaries from './modules/dictionaries'
import user from './modules/user'
import descriptionData from './modules/descriptionData'
import { request } from '../utils/public' // 字典管理
import userGroup from './modules/userGroup' // 用户分组
import landingPage from './modules/landingPage'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    permsList: null, // 按钮权限
    collapsed: false,
    scrollT: 0,
    user: null,
    phoneName: '',
    phoneImg: '',
    activityPhoneImg: '',
    phoneUrl: '',
    phoneEndUrl: '',
    phoneDescribe: '',
    valueTokey: {
      // 根据value返回中文key
      collegeType: null, // 学院名称
      shareCollegeType: null, // 分享语管理-学院名称
      shareType: null, // 类型 （图文、小视频）
      hotType: {
        VESTUSER: { name: '马甲用户', class: '' },
        REALUSER: { name: '真实用户', class: '' }
      },
      jobCourseType: {
        VIP: { name: '正式作业', class: '' },
        TEST: { name: '试学作业', class: '' }
      },
      jobWorkType: {
        TEXT: { name: '图文', class: '' },
        VIDEO: { name: '短视频', class: '' }
      },
      levelType: null, // 等级
      belongType: null, // 所属学院
      workType: null, // 生活类型
      showType: null, // 展示方法
      result: null, // 人审结果、机审结果公用
      messageType: null, // 互动消息
      operType: null, // 成功/失败（操作日志-获取操作状态）
      hideShow: null, // 显示/隐藏
      sysNormalDisable: null, // 正常/停用（部门管理-部门状态）
      sysCommonStatus: null, // 新增/编辑...（操作日志-获取操作类型）
      machineStatus: null, // 机审结果
      peopleStatus: null, // 人审结果
      personStatus: null,
      touchType: null, // 触达渠道
      sourceType: null, // 发布类型
      checkStatu: null, //审核状态
      submitStatu: null, //提交状态
      activeType: null, // 活动类型
      tagsType: null, //活动分类
      displayRule: null, //展示方式
      recommendType: null, //是否推荐
      vestList: {}, //当前登录用户，马甲列表
      jobLevel: null, //作业打分
      lifeUserPro: {
        VEST: { name: '马甲', class: '' },
        REALUSER: { name: '用户', class: '' }
      }, //马甲、用户
      userProperty: {
        VESTUSER: { name: '马甲', class: '' },
        REALUSER: { name: '用户', class: '' }
      },
      workUserPro: {
        VEST: { name: '马甲', class: '' },
        REALUSER: { name: '用户', class: '' }
      },
      //直播间礼物
      goodEnabled: {
        Y: { name: '上架', class: 'primary' },
        N: { name: '下架', class: '' }
      },
      //课程类型
      courseType: {
        PC_VIP: { name: '正式课' },
        UN_OPEN_TRIAL: { name: '非公开体验课' },
        PC_TRIAL: { name: '公开体验课' },
        TRIAL: { name: '橱窗试学课' }
      },
      //课程状态
      courseState: {
        DRAFT: { name: '未发布', class: '' },
        PUBLISHED: { name: '已发布', class: '' },
        CLOSED: { name: '已关闭', class: '' }
      },
      //学员合伙人身份等级
      grade: {
        DEFAULT: { name: '正式课学员' },
        PRIMARY: { name: '初级合伙人' },
        INTERMEDIATE: { name: '中级合伙人' },
        SENIOR: { name: '高级合伙人' }
      },
      //合伙人学员状态
      studyStatus: {
        FORMAL: { name: '正式课' },
        CHARGEBACK: { name: '已退单' }
      },
      //奖励发放状态
      grantStatus: {
        NOT_GRANT: { name: '未发放' },
        GRANT: { name: '已发放' },
        STOP: { name: '中止' }
      }
    },
    praiseSpeed: {},
    cachedViews: [], //右侧缓存页面name数组
    prizeId: {},
    vestList: {}, //当前登录用户，马甲列表
    exportLoading: {}, //导出按钮，增加loading效果
    removeLog: '', //是否删除当前tab标签
    userType: '' // 发放用户/参与用户
  },
  mutations: {
    // 操作顶部tabs选项
    setCachedViews: (state, { type = '', name = '' }) => {
      let ary = [...state.cachedViews]
      switch (type) {
        case 'add': //'新增'
          ary.push(name)
          break
        case 'del': //删除单个
          let i = ary.findIndex((item) => item === name)
          if (i >= 0) {
            ary.splice(i, 1)
          }
          break
        case 'delAll': //删除全部
          ary = []
          break
        case 'delOther': //删除全部
          ary = ary.filter((item) => item === name || item === 'main')
          break
      }
      state.cachedViews = [...ary]
      /*  console.log('state.cachedViews=' + state.cachedViews)*/
    },
    setPrizeId: (state, { activeId, prizeId }) => {
      state.prizeId[activeId] = prizeId
    },
    setVestList: (state, obj) => {
      state.vestList = obj
    },
    setExportLoading: (state, { key, value }) => {
      Vue.set(state.exportLoading, key, value)
    },
    appendPermsList(state, payload) {
      state.permsList = payload
    },
    setMenuTop: (state, mHeight) => {
      state.scrollT = mHeight
    },
    setUserInfo: (state, obj) => {
      if (!obj) {
        //清空用户数据
        localStorage.removeItem('userInfo')
        //清空数据字典
        localStorage.removeItem('dictionaries')
        //清空手机号弹出模板
        sessionStorage.removeItem('authenPageName')
        //清空菜单名称
        // sessionStorage.removeItem('menuName')
        //清空query
        sessionStorage.removeItem('query')
        //清空右侧内容区域，顶部标签
        sessionStorage.removeItem('editableTabs')
      }
      state.user = obj
    },
    setTypeNameCol: (state, { obj, name }) => {
      let temp = {}
      for (let item of obj) {
        temp[item.dictValue] = {
          name: item.dictLabel,
          class: item.listClass
        }
      }
      state['valueTokey'][name] = temp
    },
    setPhoneName: (state, value) => {
      state.phoneName = value
    },
    setPhoneImg: (state, value) => {
      state.phoneImg = value
    },
    setActivityPhoneImg: (state, value) => {
      state.activityPhoneImg = value
    },
    setPhoneUrl: (state, value) => {
      state.phoneUrl = value
    },
    setPhoneEndUrl: (state, value) => {
      state.phoneEndUrl = value
    },
    setPhoneDescribe: (state, value) => {
      state.phoneDescribe = value
    },
    setPraiseSpeed(state, { index, blockName, isAdd = true }) {
      Vue.set(state.praiseSpeed, `${blockName}_${index}`, isAdd)
    },
    setRemoveLog: (state, value) => {
      state.removeLog = value
    },
    setUserType: (state, value) => {
      state.userType = value
    }
  },
  actions: {
    setMenuScrollTop: (context, mHeight) => {
      context.commit('setMenuTop', mHeight)
    }
  },
  modules: {
    menu,
    look,
    icon,
    dictionaries,
    user,
    descriptionData,
    userGroup,
    landingPage
  },
  getters: {
    permsListGetters(state) {
      return state.permsList
    },
    menuData: (state) => {
      return state.menu.menuList
    },
    lookList: (state) => {
      return state.look.lookList
    },
    iconList: (state) => {
      return state.icon.iconList
    }
  }
})
